package com.ives.launcher;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;

import com.ives.launcher.module.DataLoader;
import com.ives.launcher.view.View_Apps_Root;
import com.ives.launcher.view.View_Cell;
import com.ives.launcher.view.View_Menu_Bottom;
import com.ives.launcher.view.View_PageIndexBar;
import com.ives.launcher.view.View_Page_Item;
import com.ives.launcher.view.View_Page_Root;
import com.ives.launcher.view.View_Root;

/**
 * 主界面
 * @author ives
 * 
 */
@SuppressLint("ParserError")
public class MainActivity extends Activity {
	private View_Root view_Root;// 桌面最外层的容器
	private View_Page_Root view_page_Root;// 工作空间，可以包含多个page_item
	private View_Apps_Root view_apps_root;
	private View_PageIndexBar pageIndexBar_desktop;
	private View_PageIndexBar pageIndexBar_app;
	
	private DataLoader loader;//数据加载器

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		/** 全屏设置，隐藏窗口所有装饰 */
		//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
		/** 标题是属于View的，所以窗口所有的修饰部分被隐藏后标题依然有效 */
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.launcher);
		loadViews();
		loadDatas();
	}


	@Override
	protected void onResume() {
		super.onResume();
		view_Root.setWallpaper(3, 5,getWindowManager().getDefaultDisplay().getWidth(),getWindowManager().getDefaultDisplay().getHeight());
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@SuppressLint("ParserError")
	private void loadViews() {
		view_Root = (View_Root) findViewById(R.id.view_root);
		pageIndexBar_desktop = (View_PageIndexBar) findViewById(R.id.control);
		view_page_Root = (View_Page_Root) findViewById(R.id.view_page_root);
		view_apps_root = (View_Apps_Root) findViewById(R.id.view_apps_page_root);

		view_Root.setActivity(this);
		view_page_Root.setView_manager(view_Root);
		view_apps_root.setView_manager(view_Root);
		view_apps_root.getView_AllApps_Root().setView_manager(view_Root);
		view_Root.setPage_root(view_page_Root);
		
		loader = new DataLoader(this,view_Root, view_page_Root, view_apps_root.getView_AllApps_Root());
		loader.setPageIndexBar_desktop(pageIndexBar_desktop);
		loader.setPageIndexBar_app(view_apps_root.getPageIndexBar());
		loader.setApps_root(view_apps_root);
		loader.setMenu_bottom((View_Menu_Bottom) findViewById(R.id.view_menu_bottom));
	}

	// 添加测试数据
	private void loadDatas() {
		loader.startLoad();
		view_apps_root.getView_AllApps_Root().setParentView(view_apps_root);
		
		/* 添加自定义位置和大小的cell */
		View_Cell v1 = new View_Cell(this, "this is a test",BitmapFactory.decodeResource(getResources(), R.drawable.test),10f, 2f);
		View_Page_Item.LayoutParams lp1 = new View_Page_Item.LayoutParams(2, 2,2, 2, 15, 15);
		// screen1.addView(v1, lp1);
		view_page_Root.addCellToScreen(1, v1, lp1);
		
	}

	/**
	 * 关闭抽屉
	 */
	public void closeSlidingDrawer() {
		view_Root.closeSlidingDrawer();
	}
	int index = 1;
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			view_Root.setWallpaper(++index,5);
			return false;
		}
		 return super.onKeyUp(keyCode, event);

	}
}

